package com.wxq.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
@EnableAsync //开启多线程
public class ThreadPoolConfig {
    @Bean("taskExcutor")
    public Executor asyncServiceExecutor(){
//        创建新的线程池对象
        ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
//        核心线程数量
        pool.setCorePoolSize(5);
//        最大线程池数量
        pool.setMaxPoolSize(20);
//        空闲线程存活时间
        pool.setKeepAliveSeconds(60);
//        队列大小
        pool.setQueueCapacity(Integer.MAX_VALUE);
//        线程名称
        pool.setThreadNamePrefix("wxq博客");
//        等待所有任务结束后再关闭线程池
        pool.setWaitForTasksToCompleteOnShutdown(true);
        //执行初始化
        pool.initialize();
        return pool;
    }
}
